package com.zsx.shopping.user.controller;

import com.zsx.shopping.common.api.R;
import com.zsx.shopping.common.api.user.vo.ReceiveAddressVO;
import com.zsx.shopping.user.annotation.ReceiveAddressPermissionCheck;
import com.zsx.shopping.user.form.ReceiveAddressForm;
import com.zsx.shopping.user.service.ReceiveAddressService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/user/receiveAddress")
public class ReceiveAddressController {

    @Resource
    private ReceiveAddressService receiveAddressService;

    @GetMapping("/receiveAddressList")
    public R<List<ReceiveAddressVO>> getReceiveAddressList() {
        return R.success(receiveAddressService.getReceiveAddressList());
    }

    @PostMapping("/receiveAddress")
    public R<List<ReceiveAddressVO>> addReceiveAddress(@Valid @RequestBody ReceiveAddressForm receiveAddressForm) {
        receiveAddressService.addReceiveAddress(receiveAddressForm);
        return R.success(receiveAddressService.getReceiveAddressList());
    }

    @ReceiveAddressPermissionCheck(id = "receiveAddressId")
    @PutMapping("/defaultReceiveAddress")
    public R<List<ReceiveAddressVO>> updateDefaultReceiveAddress(@RequestParam Long receiveAddressId) {
        receiveAddressService.updateDefaultReceiveAddress(receiveAddressId);
        return R.success(receiveAddressService.getReceiveAddressList());
    }

    @ReceiveAddressPermissionCheck(id = "receiveAddressId")
    @DeleteMapping("/receiveAddress")
    public R<List<ReceiveAddressVO>> deleteReceiveAddress(@RequestParam Long receiveAddressId) {
        receiveAddressService.deleteReceiveAddress(receiveAddressId);
        return R.success(receiveAddressService.getReceiveAddressList());
    }

    @ReceiveAddressPermissionCheck(id = "receiveAddressId")
    @GetMapping("/receiveAddress")
    public R<ReceiveAddressVO> getReceiveAddress(@RequestParam Long receiveAddressId) {
        return R.success(receiveAddressService.getReceiveAddress(receiveAddressId));
    }

    @GetMapping("/seckill/defaultReceiveAddress")
    public R<ReceiveAddressVO> getDefaultReceiveAddress(@RequestParam Long userId) {
        return R.success(receiveAddressService.getDefaultReceiveAddress(userId));
    }
}
